<template>
<div>
    <el-form size="small" :model="form" ref="ruleForm" label-width="100px">
      <div class="mt16">
        <div class="common-form">{{$t('plus.activity_ad')}}</div>
        <el-form-item label="" prop="backImage">
          <div class="d-c-c ad-picture" @click="openUpload">
            <img
              v-if="form.backImage"
              :src="form.backImage"
              width="186"
              height="128"
            />
            <span v-else
              ><el-icon><Picture /></el-icon
            ></span>
          </div>
          <p class="ml10 gray">{{$t('plus.ad_image_tip')}}</p>
        </el-form-item>
        <el-tabs v-model="activeName">
          <el-tab-pane :label="$t('plus.growth_task')" name="growTask"></el-tab-pane>
          <el-tab-pane :label="$t('plus.daily_task')" name="dayTask"></el-tab-pane>
        </el-tabs>
        <template v-if="activeName == 'growTask'">
          <div v-for="(item, index) in form.growTask" :key="index">
            <div class="common-form">{{item.name}}</div>
            <el-form-item :label="$t('plus.state')" prop="isOpen">
              <el-radio-group v-model="item.isOpen">
                <el-radio :label="1">{{$t('plus.enable')}}</el-radio>
                <el-radio :label="0">{{$t('plus.disable')}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <template v-if="item.isOpen == '1'">
              <el-form-item :label="$t('plus.reward')" prop="points">
                <div class="max-w460 d-s-c">
                  <div class="flex-1">
                    <el-input
                      v-model="item.points"
                      placeholder="0"
                      type="number"
                    ></el-input>
                  </div>
                  <span class="ml10">{{$t('plus.gift_point')}}</span>
                </div>
              </el-form-item>
              <el-form-item :label="$t('plus.rule')" prop="rule">
                <div class="max-w460 d-s-c">
                  <div class="flex-1">
                    <el-input
                      type="textarea"
                      resize="none"
                      :autosize="{  minRows: 2,  maxRows: 4}"
                      :placeholder="$t('plus.input_content')"
                      v-model="item.rule"
                    ></el-input>
                  </div>
                </div>
              </el-form-item>
            </template>
          </div>
        </template>
        <template v-if="activeName == 'dayTask'">
          <div v-for="(item, index) in form.dayTask" :key="index">
            <div class="common-form">{{item.name}}</div>
            <el-form-item :label="$t('plus.state')" prop="isOpen">
              <el-radio-group v-model="item.isOpen">
                <el-radio :label="1">{{$t('plus.enable')}}</el-radio>
                <el-radio :label="0">{{$t('plus.disable')}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <template v-if="item.isOpen == '1'">
              <el-form-item :label="$t('plus.reward')" prop="points">
                <div class="max-w460 d-s-c">
                  <div class="flex-1">
                    <el-input
                      v-model="item.points"
                      placeholder="0"
                      type="number"
                    ></el-input>
                  </div>
                  <span class="ml10">{{$t('plus.gift_point')}}</span>
                </div>
              </el-form-item>
              <el-form-item :label="$t('plus.rule')" prop="rule">
                <div class="max-w460 d-s-c">
                  <div class="flex-1">
                    <el-input
                      type="textarea"
                      resize="none"
                      :autosize="{  minRows: 2,  maxRows: 4}"
                      :placeholder="$t('plus.input_content')"
                      v-model="item.rule"
                    ></el-input>
                  </div>
                </div>
              </el-form-item>
            </template>
          </div>
        </template>
      </div>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button type="primary" size="small" @click="onSubmit"
          >{{$t('plus.submit')}}</el-button
        >
      </div>
    </el-form>
    <!--上传图片组件-->
    <Upload v-if="isupload" :isupload="isupload" @returnImgs="returnImgsFunc"
      >{{$t('plus.upload_image_button')}}</Upload
    >
  </div>
</template>

<script>
import SettingApi from "@/api/setting.js";
import Upload from "@/components/file/Upload.vue";
export default {
  components: {
    Upload
  },
  data() {
    return {
      activeName: "growTask",
      /*表单数据对象*/
      form: {
        backImage: "",
        growTask: [],
        dayTask: []
      },
      /*是否正在加载*/
      loading: true,
      isupload: false
    };
  },
  created() {
    /*获取列表*/
    this.getData();
  },
  methods: {
    /*上传*/
    openUpload(e) {
      this.isupload = true;
    },
    /*获取图片*/
    returnImgsFunc(e) {
      if (e != null && e.length > 0) {
        this.form.backImage = e[0].filePath;
      }
      this.isupload = false;
    },
    /*获取列表*/
    getData() {
      let self = this;
      self.loading = true;
      SettingApi.getTask().then(res => {
        self.loading = false;
        let val = res.data;
        self.form.backImage = val.backImage;
        self.form.growTask = val.growTask;
        self.form.dayTask = val.dayTask;
      }).catch(error => {});
    },
    /*提交签到有礼*/
    onSubmit() {
      let self = this;
      let params = self.form;
      params.growTask.forEach(e => {
        e.points = parseInt(e.points);
      });
      params.dayTask.forEach(e => {
        e.points = parseInt(e.points);
      });
      self.loading = true;
      self.$refs.ruleForm.validate(valid => {
        if (valid) {
          SettingApi.editTask(params, true).then(data => {
            ElMessage({
              message: $t("plus.save_success"),
              type: "success"
            });
            self.getData();
          }).catch(error => {});
        }
      });
    },
    /*获取富文本编辑器的内容*/
    getContent: function () {
      return this.$refs.ue.getUEContent();
    },
    /*添加奖励*/
    addReward() {
      let obj = {
        day: 0,
        is_point: false,
        point: 0,
        is_coupon: false,
        coupon: []
      };
      this.form.reward_data.push(obj);
    },
    /*删除奖励*/
    deleteRewardClick(e) {
      this.form.reward_data.splice(e, 1);
    }
  }
};
</script>

<style scoped="scoped">
.open-sign {
  padding: 20px;
  background: #e2f0ff;
}

.reward-list .reward-item {
  margin-bottom: 10px;
  padding: 20px 20px 0;
}

.reward-list .delete-reward {
  position: absolute;
  top: -10px;
  right: -10px;
  cursor: pointer;
  z-index: 10;
}

.el-button--small.is-circle {
  padding: 4px;
}
.ad-picture {
  width: 186px;
  height: 128px;
  font-size: 40px;
  color: blue;
  cursor: pointer;
  overflow: hidden;
}
.ad-picture:hover {
  border: 1px dashed blue;
}

.ad-picture img {
  object-fit: fill;
}
</style>

